<template>
<div>
  <div id="main" ref="main"></div>

</div>
</template>

<script lang="ts" setup>
import { onMounted, ref, defineProps, toRefs, watch } from "vue";
import * as echarts from "echarts";
import { echartsAutoPlay } from "@/utils/echarts";
const main = ref();

const props = defineProps({
  dataList:{
    type: Array
  }
})
const { dataList } = toRefs(props);
const data = ref([])

watch(
  dataList,
  (e) => {
    // console.log(e)
    data.value = e;
    setTimeout(()=>{
      setEcharts();
    },1000)
  },
  {
    immediate: true,
  }
);

onMounted(() => {
  // setEcharts();
});
function setEcharts() {
  if(data.value.length > 0){
    var dValue = 0
    var wValue = 0
    var zValue = 0
    data.value.forEach((item,index) => {
      dValue = dValue + Number(item.d)
      wValue = dValue + Number(item.w)
      zValue = dValue + Number(item.z)
    });
    var myChart = echarts.init(main.value);
  }
  var option = {
    tooltip: {
      extraCssText: "z-index:999",
      confine: true,
      backgroundColor: "rgba(255,255,255,0.8)"
    },
    legend: {
      top: "bottom",
      icon: "circle",
      textStyle: {
        color: "#CBD2D5",
      },
      itemWidth: 10,  // 设置宽度
      itemHeight: 10 // 设置高度
    },
    series: [
      {
        type: "pie",
        radius: ["20%", "60%"],
        center: ["50%", "50%"],
        roseType: "area",
        label: {
          show: true,
          color: "#CBD2D5",
          formatter: (e: any) => {
            return `${e.name} ${e.value}`;
          },
        },
        data: [
          { value: zValue, name: "上升" },
          { value: dValue, name: "下降" },
          { value: wValue, name: "无变化" },
        ],
      },
    ],
  };

  myChart.setOption(option);
  echartsAutoPlay(option,myChart)
}
</script>

<style lang="less" scoped>
#main {
  width: 100%;
  height: 100%;
}
</style>
